<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$basic=$this->plugin_setting('basic');
$return['gamexieyi']=$this->plugin_setting('gamexieyi');
$types=$this->item_types;
if($_RQ['op']=='main'){
	//查询在进行的项目
	$type = intval($_RQ['type']);
	$member=$this->model('member')->getinfo();
	//车主需要验证
	if($type==1){
		if($member['type']==0){
			exi('你还不是车主,也不是专线单位，请申请入驻！','error','jy_wuliu/apply//type=1');
		}
	}
	//专线需要验证
	if($type==2){
		if($member['type']!=2){
			exi('你还不是专线单位，请申请入驻！','error','jy_wuliu/apply//type=2');
		}
	}
	$isweishop = intval($_RQ['isweishop']);
	$user=pdo_get('jy_wuliu_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	//VIP时间：
	$user['vipstatus']=$user['viptime']>time()?1:2;
	if($type==8 && $basic['isfee_hongbao'] !=1 && $user['vipstatus'] !=1){
		if($user['isvip'] !=1){
			exi('只有VIP会员才能发红包主题！前往开通VIP！','error','/myhome/buyvip/');
		}else{
			exi('只有VIP会员才能发红包主题！前往续费VIP！','error','/myhome/buyvip/');
		}
	}
	$id = intval($_RQ['id']);
	if($_RQ['snsid']){
		$this->session('snsid',intval($_RQ['snsid']));
	}
	$snsid=$this->session('snsid');
	if($id){
		$this->session('pid',$id);
		$return['isid']=1;
	}else{
		$this->session('pid',0);
	}
	$return['likesnum']=pdo_getsum('jy_wuliu_item',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'likes');
	$return['looksnum']=pdo_getsum('jy_wuliu_item',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'looks');
	$return['itemnum']=pdo_count('jy_wuliu_item',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$member=$this->model('member')->getinfo();
	if($id){
		$return['post']=pdo_get('jy_wuliu_item',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$return['post']['content']=json_decode($return['post']['content'],true);
		$return['post']['diyform']=json_decode($return['post']['diyform'],true);
		$return['post']['fileds']=json_decode($return['post']['fileds'],true);
		$return['post']['canyufileds']=json_decode($return['post']['canyufileds'],true);
		$return['post']['pingxuanfileds']=json_decode($return['post']['pingxuanfileds'],true);
		$return['post']['groupbuyfloor']=json_decode($return['post']['groupbuyfloor'],true);
		$return['post']['groupsendsettitle']=$return['post']['groupsendset']==1?'快递发货':$return['post']['groupsendset']==2?'提货点自提':'没有物流';
		$type=$return['post']['type'];
		$return['type']=$type;
		$return['typetitle']=$types[$type];
		$return['mobile']=$member['mobile'];
	}else{
		$pingxuanfileds =array(
			array('title'=>'选项','type'=>'1','ismust'=>'0','value'=>'','image'=>''),
			array('title'=>'选项','type'=>'1','ismust'=>'0','value'=>'','image'=>''),
		);
		$return['post']=array();
		$return['post']['content']=array(
			array('title'=>'文字','type'=>1),
		);
		if($type==0){
			$return['post']['fileds'][0]=array();
			$return['newfileds'] = array();
		}elseif($type==1){
			$return['post']['fileds'][0]=$basic['groupfileds'];
			$return['newfileds'] = $basic['groupfileds'];
			$return['post']['groupbuyfloor'][0]=array('totle'=>6,'zhekou'=>8);
			$return['post']['groupsendset']=1;
			$return['post']['groupsendsettitle']='快递发货';
			$return['post']['canyufileds'] = array(
				array('title'=>'联系人','type'=>1,'isopen'=>1),
				array('title'=>'联系电话','type'=>8,'isopen'=>1),
				array('title'=>'联系地址','type'=>7,'isopen'=>1),
			);
		}elseif($type==3){
			$return['post']['fileds'][0]=$basic['baomingfileds'];
			$return['newfileds'] = $basic['baomingfileds'];
		}elseif($type==5){
			$return['post']['fileds']=$pingxuanfileds;
			$return['newfileds'] = $pingxuanfileds[0];
		}else{
			$return['post']['fileds']=array();
			$return['newfileds'] = array();
		}
		if($type==0 || $type==3 || $type==5){
			$return['post']['canyufileds'] = array(
				array('title'=>'别名(可点击命名：如家长姓名、学生名字、楼层等)','type'=>1,'isopen'=>1,'value'=>'可点击命名：如家长姓名、学生名字、楼层等'),
				array('title'=>'编号(可点击命名：如家长学号、工号、座号等)','type'=>1,'isopen'=>1,'value'=>'可点击命名：如家长学号、工号、座号等'),
				array('title'=>'联系人','type'=>1,'isopen'=>1),
				array('title'=>'联系电话','type'=>8,'isopen'=>1),
				array('title'=>'联系地址','type'=>7,'isopen'=>0),
			);
		}
		$return['post']['diyform']=array();
		$return['post']['type']=$type;
		$return['post']['share_set']='0';
		$return['post']['fensinfo_set']='0';
		$return['post']['msg_set']='0';
		$return['post']['groupbuytype']=0;
		$return['post']['onlinetime']=date('Y-m-d',time()+7*24*3600);
		$return['post']['groupbuyfloor']=array(array('totle'=>6,'zhekou'=>9.8));
		$return['post']['isweishop']=$isweishop;
		$return['post']['mobile']=$member['mobile'];
		$return['post']['address']=$member['address'];
		$return['post']['sendtype']=0;
		$return['post']['gettype']=0;
		$return['type']=$type;
		$return['isweishop']=$isweishop;
		$return['typetitle']=$types[$type];
	}
	$return['post']['topcd']=$return['post']['topcd']?json_decode($return['post']['topcd'],true):array('city'=>'');
	$return['post']['frompcd']=$return['post']['frompcd']?json_decode($return['post']['frompcd'],true):array('city'=>'');
	$return['send_types']=$this->send_types;
	$return['get_types']=$this->get_types;
	$return['huo_types']=$basic['huo_types']?explode('|',$basic['huo_types']):$this->huo_types;
	$return['extradata']=array();
	$member['createtime']=date('Y-m-d',$member['createtime']);
	$return['member']=$member;
	exi($return);
}
if($_RQ['op']=='post'){
	$id = $this->session('pid');
	$snsid = $this->session('snsid');
	$postdata=get_postdata();
	$pcd = $postdata['pcd'];
	$type = intval($postdata['type']);
	if(!$snsid && $pcd['province'] && $pcd['city'] && $pcd['district']){
		$postpcd = trim($pcd['province']).trim($pcd['city']).trim($pcd['district']);
		$sns = pdo_getall('jy_wuliu_sns',array('uniacid'=>$_SESSION['uniacid']));
		if($sns){
			foreach($sns as $k=>$item){
				$sndpcd = json_decode($item['pcd'],true);
				$sndpcdinfo = trim($sndpcd['province']).trim($sndpcd['city']).trim($sndpcd['district']);
				if($postpcd == $sndpcdinfo){
					$snsid = $item['id'];
					$snsinfo = $sns[$k];
					$snsinfo['pcd']=json_decode($snsinfo['pcd'],true);
				}
			}
		}
		if(empty($snsinfo)){
			$pcd = array('province'=>$pcd['province'],'city'=>$pcd['city'],'district'=>$pcd['district']);
			$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'title'=>trim($pcd['district']).'圈',
					'desc'=>trim($pcd['district']).'物流圈，分享时时动态，了解周边货运物流信息！这里有全国各地的货运物流供求信息......',
					'pcd'=>ijson_encode($pcd),
					'status'=>1,
					'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_wuliu_sns',$insert);
			$snsid=pdo_insertid();
		}
	}
		if(!$postdata['content']){
			//exi('内容不能为空！','error');
		}
		if(!$postdata['mobile']){
			exi('联系电话不能为空！','error');
		}
		if($type!=1 && !$postdata['address']){
			exi('联系地址不能为空！','error');
		}
		if(!$postdata['frompcd']['city']){
			exi('出发地城市不能为空！','error');
		}
		if(!$postdata['topcd']['city']){
			exi('目的地城市不能为空！','error');
		}
		if($type!=2 && !$postdata['weight']){
			exi('重量不能为空！','error');
		}
		if($type!=2 && !$postdata['volume']){
			exi('体积不能为空！','error');
		}
		//参与数据：
		if($type !=1 && $type !=3 && $type !=0 && $type !=5){
			$postdata['canyufileds'] =array();
		}elseif($type ==0 || $type ==5){
			unset($postdata['canyufileds'][0]);
			unset($postdata['canyufileds'][1]);
		}
		$is_show = $postdata['is_show']?1:0;
		$status = $basic['item_shenhe']?0:1;
		$fromcity=$postdata['frompcd']['city']=='市辖区'?$postdata['frompcd']['province']:$postdata['frompcd']['city'];
		$topcity=$postdata['topcd']['city']=='市辖区'?$postdata['topcd']['province']:$postdata['topcd']['city'];
		$sendtype=$this->send_types[$postdata['sendtype']];
		if($type==0){
			$huotype=$this->huo_types[$postdata['huotype']];
			$title='['.$sendtype.']:我有《'.$huotype.'》要发('.$postdata['volume'].'方,重'.$postdata['weight'].'吨左右)，从['.$fromcity.']至['.$topcity.']';
		}elseif($type==1){
			$title='['.$sendtype.']:我有货车['.$fromcity.']至['.$topcity.'],(载'.$postdata['volume'].'方,重'.$postdata['weight'].'吨左右)';
		}elseif($type==2){
			$title='[专线]:['.$fromcity.']&#8644['.$topcity.']，(载'.$postdata['volume'].'方,重'.$postdata['weight'].'吨左右)';
		}
		if($id){
			$onlinetime = intval(strtotime($postdata['onlinetime']));
			$data=array(
				'title'=>$title,
				'content'=>ijson_encode($postdata['content']),
				'frompcd'=>ijson_encode($postdata['frompcd']),
				'topcd'=>ijson_encode($postdata['topcd']),
				'sendtype'=>intval($postdata['sendtype']),
				'gettype'=>intval($postdata['gettype']),
				'share_set'=>intval($postdata['share_set']),
				'statustime'=>$statustime,
				'msg_set'=>intval($postdata['msg_set']),
				'address'=>$postdata['address'],
				'mobile'=>$postdata['mobile'],
				'topaddress'=>$postdata['topaddress'],
				'fromaddress'=>$postdata['fromaddress'],
				'price'=>intval($postdata['price']),
				'weight'=>intval($postdata['weight']),
				'volume'=>intval($postdata['volume']),
			);
			pdo_update('jy_wuliu_item',$data,array('id'=>$id));
			exi('修改成功','success','jy_wuliu/item//id='.$id);
		}else{
			$onlinetime = intval(strtotime($postdata['onlinetime']))>time()?intval(strtotime($postdata['onlinetime'])):time()+7*24*3600;
			$status=$this->setting['item_shenhe']?0:1;
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'snsid'=>$snsid,
				'type'=>intval($postdata['type']),
				'title'=>$title,
				'content'=>ijson_encode($postdata['content']),
				'frompcd'=>ijson_encode($postdata['frompcd']),
				'topcd'=>ijson_encode($postdata['topcd']),
				'sendtype'=>intval($postdata['sendtype']),
				'gettype'=>intval($postdata['gettype']),
				'share_set'=>intval($postdata['share_set']),
				'statustime'=>$statustime,
				'msg_set'=>intval($postdata['msg_set']),
				'address'=>$postdata['address'],
				'mobile'=>$postdata['mobile'],
				'topaddress'=>$postdata['topaddress'],
				'fromaddress'=>$postdata['fromaddress'],
				'price'=>intval($postdata['price']),
				'weight'=>intval($postdata['weight']),
				'volume'=>intval($postdata['volume']),
				'status'=>$status,
				'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_wuliu_item',$insert);
			$id=pdo_insertid();
			if($postdata['payid']){
				$oid=pdo_getcolumn('core_paylog',array('id'=>intval($postdata['payid']),'uniacid'=>$_SESSION['uniacid']),'oid');
				pdo_update('jy_wuliu_redlog',array('pid'=>$id),array('id'=>$oid,'uniacid'=>$_SESSION['uniacid']));
			}
			if($basic['item_shenhe']){
				exi('发布成功,请等待审核','success','jy_wuliu/item//id='.$id);
			}else{
				exi('发布成功','success','jy_wuliu/item//id='.$id);
			}
			
		}
}
if($_RQ['op']=='payred'){
	$post=get_postdata();
	if(!$post['wantmoney']){
		exi('请先设置充值金额！','error');
	}
	$fee=$post['wantmoney'];
	if($basic['isideal_money']){
		if($fee<=0){
		   exi('红包金额需大于0元','error');
		}
	}else{
		if($fee<=1){
		   exi('红包金额需大于1元','error');
		}
		if($fee>200){
			   exi('单个红包金额最多不超过200元','error');
		}
		if($post['rednum']<3 || $post['rednum']>20){
			 exi('红包个数介于（包括）3-20之间','error');
		}
		if($fee/$post['rednum']<=1){
			 exi('红包平均数，不能小于1元','error');
		}
	}
	
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>0,
		'pid'=>0,
		'setmid'=>$_SESSION['uid'],
		'status'=>0,
		'fee'=>$fee,
		'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_wuliu_redlog',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'payred',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'发送红包',
		'plugin'=>'jy_wuliu',
		//'pay_type'=>'wechat',
		'back_url'=>'jy_wuliu/upitem/pay_result/'
	);
	$pay_id=cfc('pay')->create_pay_order($params);
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$redlog=pdo_get('jy_wuliu_redlog',array('id'=>$_RQ['pay_result']['id'],'uniacid'=>$_SESSION['uniacid'],'setmid'=>$_SESSION['uid']),array('id','fee','status'));
	if($redlog['fee']!=$_RQ['pay_result']['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($redlog['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($_RQ['pay_result']['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('jy_wuliu_redlog',array('status'=>1),array('id'=>$redlog['id'],'uniacid'=>$_SESSION['uniacid']));
	exi('充值成功！');
}